package com.assessment.service.impl;

import com.assessment.entity.*;
import com.assessment.mapper.*;
import com.assessment.service.AitemService;
import com.assessment.vo.AConfig;
import com.assessment.vo.AitemVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鑫十一
 * @since 2021-12-15
 */
@Service
@Transactional
public class AitemServiceImpl extends ServiceImpl<AitemMapper, Aitem> implements AitemService {
    private static final Logger log1 = LoggerFactory.getLogger(AitemServiceImpl.class);
    @Autowired(required = false)
    private AsettingMapper asettingMapper;
    @Autowired(required = false)
    private AscoreMapper ascoreMapper;
    @Autowired(required = false)
    private ApeoMapper apeoMapper;
    @Autowired(required = false)
    private AitemavgMapper aitemavgMapper;
    @Autowired(required = false)
    private AtypeMapper atypeMapper;
    @Autowired(required = false)
    private AcomsetMapper acomsetMapper;

    @Override
    public List<Column> getColumns() {
        List<Column> columns = new ArrayList<>();
        columns.add(new Column().setLabel("考核名").setProp("sname"));
        columns.add(new Column().setLabel("考核项名").setProp("name"));
        return columns;
    }

    @Override
    public boolean saveItem(AitemVo item) throws Exception {
        Asetting asetting = asettingMapper.selectById(item.getSid()); // 所属考核设置
        Aitem aitem = new Aitem().setSid(item.getSid()).setSname(asetting.getName()).setName(item.getName());
        this.save(aitem);
        List<Adep> depList = item.getDepList(); // 被评部门列表
        List<AConfig> configList = item.getConfigList(); // 打分人要求
        List<String> typeRowList = item.getTypeRowList(); // 打分项类型
        List<List<String>> roleList = item.getRoleList();// 打分人角色

        for (int i = 0; i < depList.size(); i++) {
            AConfig aConfig = configList.get(i);
            if (!aConfig.getSelf()
                    && !aConfig.getDifferent()
                    && !aConfig.getMutual()
                    && !aConfig.getSame()){
                continue;
            }
            // 添加成绩
            Adep adep = depList.get(i);
            String s = typeRowList.get(i);
            List<String> roleStrings = roleList.get(i);
            Atype atype = atypeMapper.selectById(s);
            QueryWrapper<Apeo> apeoQueryWrapper = new QueryWrapper<>();
            if (roleStrings.size()!=0){
                apeoQueryWrapper.in("rid",roleStrings);
            }
            if (!(aConfig.getSame() && aConfig.getDifferent())){
                if (aConfig.getSame()){ apeoQueryWrapper.eq("drid", adep.getRid()); } // 同组
                if (aConfig.getDifferent()){ apeoQueryWrapper.notIn("drid", adep.getRid()); } // 不同组
            }
            if (!(aConfig.getSelf() && aConfig.getMutual())){
                if (aConfig.getSelf()){ apeoQueryWrapper.eq("did", adep.getId()); } // 自评
                if (aConfig.getMutual()){ apeoQueryWrapper.notIn("did", adep.getId()); } // 互评
            }
            List<Apeo> apeos = apeoMapper.selectList(apeoQueryWrapper);
            for (Apeo apeo : apeos) {
                log1.info(apeo.toString());
                Ascore ascore = new Ascore();
                ascore.setApid(apeo.getId()).setApname(apeo.getName())
                        .setBpid(adep.getId()).setBpname(adep.getName()).setDsit(adep.getDsit()).setBrsit(adep.getRsit()).setFurl(adep.getFurl()).setFname(adep.getFname())
                        .setScore("无")
                        .setStatus("未打分").setItid(aitem.getId()).setItname(aitem.getName())
                        .setAid(aitem.getSid()).setAname(aitem.getSname())
                        .setTname(atype.getName()).setTid(atype.getId()).setTscore(atype.getScore()).setTmaxnum(atype.getMaxnum()).setTscorename(atype.getSname()).setTnum(atype.getNum())
                        .setArole(apeo.getRname()).setArid(apeo.getRid()).setArtype(apeo.getRtype())
                        .setBrole(adep.getRname()).setBrid(adep.getRid()).setBrtype(adep.getRtype());
                log1.info(ascore.toString());
                ascoreMapper.insert(ascore);
            }

            // 添加平均成绩
            Aitemavg aitemavg = new Aitemavg().setAid(aitem.getSid()).setAname(aitem.getSname())
                    .setItid(aitem.getId()).setItname(aitem.getName())
                    .setPid(adep.getId()).setPname(adep.getName())
                    .setScore("暂无")
                    .setPrid(adep.getRid())
                    .setProle(adep.getRname())
                    .setPsit(adep.getDsit())
                    .setPrtype(adep.getRtype());
            aitemavgMapper.insert(aitemavg);
        }

        return true;
    }

    @Override
    public boolean removeItem(List<String> idList) {
        acomsetMapper.delete(new QueryWrapper<Acomset>().in("itid",idList));
        ascoreMapper.delete(new QueryWrapper<Ascore>().in("itid",idList));
        aitemavgMapper.delete(new QueryWrapper<Aitemavg>().in("itid",idList));
        return this.removeByIds(idList);
    }
}
